<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录 - Demo系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .login-container {
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            padding: 40px;
            width: 100%;
            max-width: 400px;
            position: relative;
            overflow: hidden;
        }

        .login-container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, #667eea, #764ba2);
        }

        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }

        .login-title {
            color: #333;
            font-size: 28px;
            font-weight: 600;
            margin-bottom: 8px;
        }

        .login-subtitle {
            color: #666;
            font-size: 14px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-label {
            display: block;
            margin-bottom: 8px;
            color: #333;
            font-weight: 500;
            font-size: 14px;
        }

        .form-input {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #e1e5e9;
            border-radius: 10px;
            font-size: 16px;
            transition: all 0.3s ease;
            background-color: #f8f9fa;
        }

        .form-input:focus {
            outline: none;
            border-color: #667eea;
            background-color: white;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .form-input.error {
            border-color: #dc3545;
        }

        .error-message {
            color: #dc3545;
            font-size: 12px;
            margin-top: 5px;
            display: none;
        }

        .login-button {
            width: 100%;
            padding: 14px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-top: 10px;
        }

        .login-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
        }

        .login-button:active {
            transform: translateY(0);
        }

        .login-button:disabled {
            background: #ccc;
            cursor: not-allowed;
            transform: none;
        }

        .login-footer {
            text-align: center;
            margin-top: 30px;
            padding-top: 20px;
            border-top: 1px solid #e1e5e9;
        }

        .footer-text {
            color: #666;
            font-size: 14px;
        }

        .footer-link {
            color: #667eea;
            text-decoration: none;
            font-weight: 500;
            transition: color 0.3s ease;
        }

        .footer-link:hover {
            color: #764ba2;
        }

        .loading {
            display: none;
            text-align: center;
            margin-top: 10px;
        }

        .loading-spinner {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 2px solid #f3f3f3;
            border-top: 2px solid #667eea;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .alert {
            padding: 12px 16px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-size: 14px;
            display: none;
        }

        .alert-success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .alert-error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        @media (max-width: 480px) {
            .login-container {
                padding: 30px 20px;
            }
            
            .login-title {
                font-size: 24px;
            }
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-header">
            <h1 class="login-title">欢迎回来</h1>
            <p class="login-subtitle">请输入您的账号信息</p>
        </div>

        <div id="alert" class="alert"></div>

        <form id="loginForm">
            <div class="form-group">
                <label for="username" class="form-label">用户名</label>
                <input type="text" id="username" name="username" class="form-input" required>
                <div class="error-message" id="usernameError">请输入用户名</div>
            </div>

            <div class="form-group">
                <label for="password" class="form-label">密码</label>
                <input type="password" id="password" name="password" class="form-input" required>
                <div class="error-message" id="passwordError">请输入密码</div>
            </div>

            <button type="submit" class="login-button" id="loginBtn">
                登录
            </button>

            <div class="loading" id="loading">
                <div class="loading-spinner"></div>
                <span style="margin-left: 10px; color: #666;">正在登录...</span>
            </div>
        </form>

        <div class="login-footer">
            <p class="footer-text">
                还没有账号？
                <a href="register.html" class="footer-link">立即注册</a>
            </p>
        </div>
    </div>

    <script>
        // API基础URL
        const API_BASE_URL = 'http://localhost:8080/api';

        // DOM元素
        const loginForm = document.getElementById('loginForm');
        const loginBtn = document.getElementById('loginBtn');
        const loading = document.getElementById('loading');
        const alert = document.getElementById('alert');

        // 表单提交处理
        loginForm.addEventListener('submit', async function(e) {
            e.preventDefault();
            
            // 清除之前的错误状态
            clearErrors();
            hideAlert();
            
            // 获取表单数据
            const formData = new FormData(loginForm);
            const loginData = {
                username: formData.get('username').trim(),
                password: formData.get('password').trim()
            };
            
            // 客户端验证
            if (!validateForm(loginData)) {
                return;
            }
            
            // 显示加载状态
            showLoading(true);
            
            try {
                // 发送登录请求
                const response = await fetch(`${API_BASE_URL}/user/login`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(loginData)
                });
                
                const result = await response.json();
                
                if (result.code === 0) {
                    // 登录成功
                    showAlert('登录成功，即将跳转...', 'success');
                    
                    // 解析登录响应数据
                    const loginData = result.data;
                    const user = loginData.user;
                    const token = loginData.token;
                    const refreshToken = loginData.refreshToken;
                    const roles = loginData.roles;
                    const permissions = loginData.permissions;
                    
                    // 保存用户信息和认证信息到localStorage
                    localStorage.setItem('user', JSON.stringify(user));
                    localStorage.setItem('token', token);
                    localStorage.setItem('refreshToken', refreshToken);
                    localStorage.setItem('userRoles', JSON.stringify(roles));
                    localStorage.setItem('userPermissions', JSON.stringify(permissions));
                    
                    console.log('登录成功，用户角色：', roles);
                    console.log('用户权限数量：', permissions.length);
                    
                    // 延迟跳转到博客主页
                    setTimeout(() => {
                        window.location.href = 'blog.html';
                    }, 1500);
                } else {
                    // 登录失败
                    showAlert(result.message || '登录失败', 'error');
                }
            } catch (error) {
                console.error('登录请求失败:', error);
                showAlert('网络错误，请稍后重试', 'error');
            } finally {
                showLoading(false);
            }
        });

        // 表单验证
        function validateForm(data) {
            let isValid = true;
            
            if (!data.username) {
                showFieldError('username', '请输入用户名');
                isValid = false;
            }
            
            if (!data.password) {
                showFieldError('password', '请输入密码');
                isValid = false;
            }
            
            return isValid;
        }

        // 显示字段错误
        function showFieldError(field, message) {
            const input = document.getElementById(field);
            const error = document.getElementById(field + 'Error');
            
            input.classList.add('error');
            error.textContent = message;
            error.style.display = 'block';
        }

        // 清除错误状态
        function clearErrors() {
            const inputs = document.querySelectorAll('.form-input');
            const errors = document.querySelectorAll('.error-message');
            
            inputs.forEach(input => input.classList.remove('error'));
            errors.forEach(error => error.style.display = 'none');
        }

        // 显示/隐藏加载状态
        function showLoading(show) {
            if (show) {
                loginBtn.style.display = 'none';
                loading.style.display = 'block';
            } else {
                loginBtn.style.display = 'block';
                loading.style.display = 'none';
            }
        }

        // 显示提示信息
        function showAlert(message, type) {
            alert.textContent = message;
            alert.className = `alert alert-${type}`;
            alert.style.display = 'block';
        }

        // 隐藏提示信息
        function hideAlert() {
            alert.style.display = 'none';
        }

        // 输入框焦点事件 - 清除错误状态
        document.querySelectorAll('.form-input').forEach(input => {
            input.addEventListener('focus', function() {
                this.classList.remove('error');
                const errorElement = document.getElementById(this.id + 'Error');
                if (errorElement) {
                    errorElement.style.display = 'none';
                }
            });
        });

        // 页面加载时检查是否已登录
        window.addEventListener('load', function() {
            const user = localStorage.getItem('user');
            if (user) {
                // 已登录，直接跳转到主页
                window.location.href = 'index.html';
            }
        });
    </script>
</body>
</html>
